package com.gomtel.smartdevice.receiver;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.ui.activity.HomeActivity;
import com.gomtel.smartdevice.utils.CommonUtils;

import java.util.Objects;

public class LocalBroadcastReceiver extends BroadcastReceiver {

    private HomeActivity homeActivity;

    public LocalBroadcastReceiver(HomeActivity homeActivity) {
        this.homeActivity = homeActivity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (Objects.requireNonNull(intent.getAction())) {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                switch (blueState) {
                    case BluetoothAdapter.STATE_TURNING_ON:
                        break;
                    case BluetoothAdapter.STATE_ON:
                        //EventBusUtil.sendEvent(new CommonEvent(EventCode.LOCAL_AREA_LINKS));
                        if (homeActivity != null && !homeActivity.isFinishing()) {
                            homeActivity.setConnect();
                        }
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                        bluetoothAdapter.enable();
                        CommonUtils.showToast(WatchApplication.getInstance(), "请打开蓝牙");
                        break;
                }
                break;
        }
    }
}
